home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / us20src.zip / COMMON.C < prev    next >
C/C++ Source or Header  |  1992-06-26  |  2KB  |  106 lines

  1. /*    COMMON:    Most Common word processing for MicroSPELL 2.0
  2.         Spell Checker and Corrector
  3.  
  4.         (C)opyright May 1987,1992 by Daniel Lawrence
  5.         All Rights Reserved
  6. */
  7.  
  8. #include    <stdio.h>
  9. #include    "dopt.h"
  10. #include    "dstruct.h"
  11. #include    "ddef.h"
  12.  
  13. loadcom()    /* load the most used words in to the common list */
  14.  
  15. {
  16.     register char *sp;    /* temp string pointer */
  17.     register int i;        /* index */
  18.     FILE *cf;        /* ptr to common word list file */
  19.     char buf[NSTRING+1];    /* buffer to hold current word */
  20.  
  21.     /* if we are already loaded....don't bother again */
  22.     if (commonf == TRUE)
  23.         return(TRUE);
  24.  
  25.     /* try to open it.... */
  26.     if ((cf = popen(comlist, "ra")) == NULL)
  27.         return(FALSE);
  28.  
  29.     /* and load them up */
  30.     commonf = TRUE;
  31.     while (numcom < MAXCOM) {
  32.         /* get the next word */
  33.         if (fgets(buf, NSTRING, cf) == NULL)
  34.             break;
  35.         buf[strlen(buf)-1] = 0;    /* get rid of the newline */
  36.  
  37.         /* get room for it...*/
  38.         if ((sp = malloc(strlen(buf)+1)) == NULL) {
  39.             fclose(cf);
  40.             return(FALSE);
  41.         }
  42.  
  43.         /* and store it */
  44.         strcpy(sp, buf);
  45.         numcom++;
  46.         cword[numfiltr++] = sp;
  47.     }
  48.  
  49.     fclose(cf);
  50.     if (swdebug)
  51.         printf("[%u common words loaded]\n", numcom);
  52.     return(TRUE);
  53. }
  54.  
  55. dumpcom()    /* unload the common word list */
  56.  
  57. {
  58.     /* only bother if we are really here */
  59.     if (commonf == FALSE)
  60.         return(TRUE);
  61.  
  62.     /* dump them */
  63.     while (numfiltr--)
  64.         free(cword[numcom]);
  65.     if (swdebug)
  66.         printf("[common word list dumped]");
  67.     commonf = FALSE;
  68.     return(TRUE);
  69. }
  70.  
  71. iscom(word)    /* is a word in the common/user word list? */
  72.  
  73. char *word;    /* word to check */
  74.  
  75. {
  76.     register int lower;    /* lower limit for search */
  77.     register int upper;    /* upper limit for search */
  78.     register int mid;    /* current word in search */
  79.     register int cresult;    /* result of current compare */
  80.  
  81.     /* set up limits */
  82.     lower = 0;
  83.     upper = numfiltr - 1;
  84.  
  85.     /* Binary search the list */
  86.     while (upper >= lower) {
  87.  
  88.         /* find the current mid item */
  89.         mid = (upper + lower) / 2;
  90.  
  91.         /* compare it */
  92.         cresult = lowcmp(word, cword[mid]);
  93.         if (cresult == 0)    /* success!!!! */
  94.             return(TRUE);
  95.  
  96.         /* reset the search bounds */
  97.         if (cresult > 0)
  98.             lower = mid + 1;
  99.         else
  100.             upper = mid - 1;
  101.     }
  102.  
  103. fail:    /* it's not here, jim.... */
  104.     return(FALSE);
  105. }
  106.